#include "ocrobject.h"

OcrObject::OcrObject(QObject *parent) : QObject(parent)
{
    doOcr();

}

void OcrObject::doOcr()
{
    KyInfo() << "begin to run ocr thread !\n";
    tesseract::TessBaseAPI  *api = new tesseract::TessBaseAPI();

    if (api->Init(NULL, "chi_sim")) {
        KyInfo() << "Could not initialize tesseract.\n";
        g_sane_object->ocrOutputText = tr("Unable to read text");

        g_user_signal->toolbarOcrOperationFinished();
        return;
    }

    KyInfo() << "before pixRead.";
    Pix *image = pixRead(ScanningPicture);
    if (! image) {
        KyInfo() << "pixRead error!";
        g_sane_object->ocrOutputText = tr("Unable to read text");
        if (api)
            api->End();

        g_user_signal->toolbarOcrOperationFinished();

        return;
    }
    if (image) {
        KyInfo() << "before setImage.";
        api->SetImage(image);
        g_sane_object->ocrOutputText = api->GetUTF8Text();
    }

    KyInfo() << "before destroy image.";
    if (api) {
        api->End();
    }
    if (image) {
        pixDestroy(&image);
    }

    g_user_signal->toolbarOcrOperationFinished();

}
